import javax.swing.*;
import java.awt.event.*;
import java.io.File;

final class FindNextItem extends AbstractAction{
    private final Doer frame;
    int index;

    FindNextItem(String name,Doer frame){
    super(name);
    this.frame=frame;
    String iconFileName="images"+File.separator+name+".gif";
    if(new File(iconFileName).exists())putValue(SMALL_ICON,new ImageIcon(iconFileName));
  }
    public void actionPerformed(ActionEvent e){
      String word=frame.getWord();
      if(word==null) JOptionPane.showMessageDialog(frame,"Can't find the word");
      else{
        InterWindow interWindow=(InterWindow)(frame.getTab().getComponentAt(frame.getSelect()));
        JTextArea textArea=interWindow.getTextArea();
        String text=textArea.getText();
	if(index==-1)index=text.indexOf(word,index+1);
        index=text.indexOf(word,index+1);
	System.out.println(index);
	if(index<0){
	  int flag=JOptionPane.showConfirmDialog(frame,"Search has reached the end of the file.\nResearch?","Confirm Research",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
	  if(flag==JOptionPane.NO_OPTION){
	    index=-1;
	    return;
	  }else if(flag==JOptionPane.YES_OPTION)index=text.indexOf(word);
        }
        textArea.requestFocus();
        textArea.select(index,index+word.length());
      }
    }
  }
